package com.pospi.xstore.ui.caigou;

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.dfqin.grantor.PermissionListener;
import com.github.dfqin.grantor.PermissionsUtil;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.lany.sp.SPHelper;
import com.mylhyl.circledialog.CircleDialog;
import com.pospi.xstore.R;
import com.pospi.xstore.adapter.BillDetailAdapter;
import com.pospi.xstore.dao.GoodsDao;
import com.pospi.xstore.dto.CustomerBeen;
import com.pospi.xstore.dto.GoodsBeen;
import com.pospi.xstore.dto.GoodsDto;
import com.pospi.xstore.dto.StoreBeen;
import com.pospi.xstore.ui.CaptureActivity;
import com.pospi.xstore.ui.base.BaseActivity;
import com.pospi.xstore.ui.cash_ui.PointSelectActivity;
import com.pospi.xstore.ui.count.CountActivity;
import com.pospi.xstore.util.Constant;
import com.pospi.xstore.util.DoubleSave;
import com.pospi.xstore.util.GetData;
import com.pospi.xstore.util.SaveOrderInfo;
import com.pospi.xstore.view.swipemenulistview.SwipeMenu;
import com.pospi.xstore.view.swipemenulistview.SwipeMenuCreator;
import com.pospi.xstore.view.swipemenulistview.SwipeMenuItem;
import com.pospi.xstore.view.swipemenulistview.SwipeMenuListView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CgshDetailActivity extends BaseActivity {
    @Bind(R.id.time_select)
    TextView time_select;
    @Bind(R.id.billno_xz)
    TextView billno_xz;
    @Bind(R.id.cgmd_xz)
    TextView cgmd_xz;
    @Bind(R.id.gys_xz)
    TextView gys_xz;
    @Bind(R.id.cgy_xz)
    TextView cgy_xz;
    @Bind(R.id.beizhu_xz)
    TextView beizhu_xz;
    @Bind(R.id.lv_goods_cgsh)
    SwipeMenuListView lv_goods_counts;
//    @Bind(R.id.et_code_counts)
//    EditText et_code_counts;
    private GoodsDao dao;
    private List<GoodsBeen> goodsDtos = new ArrayList<>();

    String[] data = new String[]{"1号门店", "2号门店", "3号门店", "4号门店"};
    String[] data2 = new String[]{"审核", "未审核"};
    private BillDetailAdapter adapter;
    private List<StoreBeen> storeBeens;
    private List<CustomerBeen> customerBeens;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.cgsh_detail);
        ButterKnife.bind(this);
        setTitle("采购收货单新增");

        dao = new GoodsDao(getApplicationContext());
        time_select.setText(GetData.getYYMMDDTime());
        adapter = new BillDetailAdapter(this, goodsDtos);
        lv_goods_counts.setAdapter(adapter);
        setSwipeListView();

    }

    @Override
    protected void onStart() {
        super.onStart();
//        et_code_counts.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                Log.i("num", "ooo");
//                search(et_code_counts.getText().toString().trim());
//                return false;
//            }
//        });
    }
    private void check(GoodsBeen gd){
        for (GoodsBeen dto : goodsDtos) {
            if (dto.getId().equals(gd.getId())) {
                dto.setSl(DoubleSave.doubleSaveTwo(dto.getSl() + 1));
                return;
            }
        }
        goodsDtos.add(gd);

    }
    private void search(String code) {
        //搜索
//        GoodsDto goodsDto = dao.selectGoods(code);
//        if (goodsDto != null) {
//            check(goodsDto);
//        } else {
//            Toast.makeText(this, "商品不存在", Toast.LENGTH_SHORT).show();
////
//        }
////        et_code_counts.setText("");
//        adapter.notifyDataSetChanged();
//        lv_goods_counts.setFocusable(false);
//        ETRequestFocus();

    }
//    public void ETRequestFocus() {
//        et_code_counts.setFocusable(true);
//        et_code_counts.setFocusableInTouchMode(true);
//        et_code_counts.requestFocus();
//    }
    private void creatDialog(List<String> ss) {

        new CircleDialog.Builder(this)
                .setItems(ss, new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                    }
                })
                .setGravity(Gravity.CENTER)
                .show();
    }
    private void requestCemera() {
        if (PermissionsUtil.hasPermission(this, Manifest.permission.CAMERA)) {
            //有访问摄像头的权限
            //扫描
            Intent intent = new Intent(CgshDetailActivity.this, CaptureActivity.class);
            startActivityForResult(intent, 1);
        } else {
            PermissionsUtil.requestPermission(this, new PermissionListener() {
                @Override
                public void permissionGranted(@NonNull String[] permissions) {
                    //用户授予了访问摄像头的权限
                    //扫描
                    Intent intent = new Intent(CgshDetailActivity.this, CaptureActivity.class);
                    startActivityForResult(intent, 1);
                }


                @Override
                public void permissionDenied(@NonNull String[] permissions) {
                    //用户拒绝了访问摄像头的申请
                }
            }, new String[]{Manifest.permission.CAMERA});
        }
    }
    List<String> strings = new ArrayList<>();

    @OnClick({R.id.time_select,R.id.cgmd_xz,R.id.gys_xz,R.id.cgy_xz})
    public void onClick(View view) {
        switch (view.getId()) {
//            case R.id.iv_search_xz:
//                startActivity(PointSelectActivity.class);
//                break;
//            case R.id.iv_scan_xz:
//                requestCemera();
//                break;
            case R.id.cgmd_xz:
                storeBeens = SaveOrderInfo.changeStoreToList(SPHelper.getInstance().getString(Constant.STORE_LIST));
                strings.clear();
                for (StoreBeen been : storeBeens) {
                    strings.add(been.getName());
                }
                creatDialog(strings);

                break;
            case R.id.gys_xz:
                startActivity(SupActivity.class);

                break;
            case R.id.cgy_xz:
                customerBeens = SaveOrderInfo.changeCusToList(SPHelper.getInstance().getString(Constant.CUS_LIST));
                strings.clear();
                for (CustomerBeen been : customerBeens) {
                    strings.add(been.getName());
                }
                creatDialog(strings);
                break;
            case R.id.time_select:
                TimePickerDialog dialogYearMonthDay2 = new TimePickerDialog.Builder()
                        .setType(Type.YEAR_MONTH_DAY)
                        .setTitleStringId("选择日期")
                        .setWheelItemTextSize(16)
                        .setCallBack(new OnDateSetListener() {
                            @Override
                            public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                                Date date = new Date(millseconds);
                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                                String time = format.format(date);
                                time_select.setText(time);
                            }
                        })
                        .build();
                dialogYearMonthDay2.show(getSupportFragmentManager(), "YEAR_MONTH_DAY");
                break;

        }
    }
    private void setSwipeListView() {
        // step 1. create a MenuCreator
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                // create "open" item
                SwipeMenuItem openItem = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
                openItem.setBackground(new ColorDrawable(Color.rgb(0xD9, 0x47,
                        0x47)));
                // set item width
                openItem.setWidth(dp2px(90));
                // set item title
                openItem.setTitle("删除");
                // set item title fontsize
                openItem.setTitleSize(18);
                // set item title font color
                openItem.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(openItem);
            }
        };
        // set creator
        lv_goods_counts.setMenuCreator(creator);

        // step 2. listener item click event
        lv_goods_counts.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(int position, SwipeMenu menu, int index) {
//                GoodsDto item = goodsDtos.get(position);
//					delete(item);
                goodsDtos.remove(position);
                adapter.notifyDataSetChanged();
            }
        });

        // set SwipeListener
        lv_goods_counts.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {

            @Override
            public void onSwipeStart(int position) {
                // swipe start
            }

            @Override
            public void onSwipeEnd(int position) {
                // swipe end
            }
        });
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }
}
